package com.xiaoxu.intranetweb.mybatisplusSzappdb.business.auth;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.SysIntranetWebMenu;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.ISysIntranetWebMenuService;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.vo.Constructor.SysIntranetWebMenu2GetWebMenuVo;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.vo.GetWebMenuVo;
import com.xiaoxu.intranetweb.util.SzResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

@Component
public class WebMenuBu {
//    菜单管理
    @Autowired
    private ISysIntranetWebMenuService sysIntranetWebMenuService;
    @Autowired
    private SysIntranetWebMenu2GetWebMenuVo sysIntranetWebMenu2GetWebMenuVo;
public List<GetWebMenuVo> getWebMenu() {
    List<GetWebMenuVo> list = new ArrayList<>();
    QueryWrapper<SysIntranetWebMenu> queryWrapper = new QueryWrapper<>();
    queryWrapper.isNull("WEB_PAGE_PARENT");
    queryWrapper.orderByAsc("WEB_PAGE_NUMBER");
    List<SysIntranetWebMenu> level1List = sysIntranetWebMenuService.list(queryWrapper);
    for (SysIntranetWebMenu level1 : level1List) {
        GetWebMenuVo level1Vo = sysIntranetWebMenu2GetWebMenuVo.GetWebMenuVo(level1);
        QueryWrapper<SysIntranetWebMenu> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("WEB_PAGE_ROUTELEVEL", 2);
        queryWrapper2.eq("WEB_PAGE_PARENT", level1.getWebPageId());
        queryWrapper2.orderByAsc("WEB_PAGE_NUMBER");
        List<SysIntranetWebMenu> level2List = sysIntranetWebMenuService.list(queryWrapper2);
        List<GetWebMenuVo> level2VoList = new ArrayList<>();
        for (SysIntranetWebMenu level2 : level2List) {
            GetWebMenuVo level2Vo = sysIntranetWebMenu2GetWebMenuVo.GetWebMenuVo(level2);
            QueryWrapper<SysIntranetWebMenu> queryWrapper3 = new QueryWrapper<>();
            queryWrapper3.eq("WEB_PAGE_ROUTELEVEL", 3);
            queryWrapper3.eq("WEB_PAGE_PARENT", level2.getWebPageId());
            queryWrapper3.orderByAsc("WEB_PAGE_NUMBER");
            List<SysIntranetWebMenu> level3List = sysIntranetWebMenuService.list(queryWrapper3);
            List<GetWebMenuVo> level3VoList = new ArrayList<>();
            for (SysIntranetWebMenu level3 : level3List) {
                GetWebMenuVo level3Vo = sysIntranetWebMenu2GetWebMenuVo.GetWebMenuVo(level3);
                level3VoList.add(level3Vo);
            }
            level2Vo.setChildren(level3VoList);
            level2VoList.add(level2Vo);
        }
        level1Vo.setChildren(level2VoList);
        list.add(level1Vo);
    }
    return list;
}
//新增菜单
public SzResult addWebMenu(GetWebMenuVo getWebMenuVo) {
    SysIntranetWebMenu sysIntranetWebMenu = sysIntranetWebMenu2GetWebMenuVo.sysIntranetWebMenu(getWebMenuVo);
    sysIntranetWebMenuService.save(sysIntranetWebMenu);
    return SzResult.Success();
}
//修改菜单
public SzResult updateWebMenu(GetWebMenuVo getWebMenuVo) {
    SysIntranetWebMenu sysIntranetWebMenu = sysIntranetWebMenu2GetWebMenuVo.sysIntranetWebMenu(getWebMenuVo);
    sysIntranetWebMenuService.updateById(sysIntranetWebMenu);
    return SzResult.Success();
}
}
